package com.bw.ijk.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.bumptech.glide.Glide;
import com.bw.ijk.R;
import com.bw.ijk.widget.media.IjkVideoView;

import tv.danmaku.ijk.media.player.IMediaPlayer;


public class VideoView extends FrameLayout implements LifecycleObserver,
        IMediaPlayer.OnCompletionPreparedListener,View.OnClickListener {

    private IjkVideoView ijkVideoView;
    private ImageView imageView;
    private RelativeLayout relativeLayout;
    private ImageView playImg;

    public VideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        AppCompatActivity appCompatActivity = (AppCompatActivity) context;
        appCompatActivity.getLifecycle().addObserver(this);
        init();
    }

    //初始化控件
    private void init(){
        ijkVideoView = new IjkVideoView(getContext());
        ijkVideoView.setmOnCompletionPreparedListener(this);
        imageView = new ImageView(getContext());
        relativeLayout = new RelativeLayout(getContext());
        playImg = new ImageView(getContext());
        LayoutParams ijkVideoLp = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        ijkVideoView.setLayoutParams(ijkVideoLp);
        LayoutParams imgLp = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(imgLp);
        LayoutParams relativeLp = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(relativeLp);
        RelativeLayout.LayoutParams playImgLp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        playImgLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        playImg.setLayoutParams(playImgLp);
        playImg.setImageResource(R.drawable.icon_video_pause);
        relativeLayout.addView(playImg);
        addView(ijkVideoView);
        addView(imageView);
        addView(relativeLayout);
        playImg.setOnClickListener(this);
    }

    //设置播放地址
    public void setPlayUrl(String path,String imgUrl){
        if (ijkVideoView == null)
            return;
        if (!TextUtils.isEmpty(path))
            ijkVideoView.setVideoPath(path);
        if (!TextUtils.isEmpty(imgUrl)){
            Glide.with(getContext()).load(imgUrl).into(imageView);
        }
    }

    public void setPlayUrl(String path){
        if (ijkVideoView == null)
            return;
        if (!TextUtils.isEmpty(path))
            ijkVideoView.setVideoPath(path);
    }

    public void setImgUrl(String imgUrl){
        if (!TextUtils.isEmpty(imgUrl)){
            Glide.with(getContext()).load(imgUrl).into(imageView);
        }
    }

    //开始播放
    public void start(){
        if (ijkVideoView == null)
            return;
        playImg.setImageResource(R.drawable.icon_video_pause);
        ijkVideoView.start();
    }
    //暂停播放
    public void pause(){
        if (ijkVideoView == null)
            return;
        if (ijkVideoView.canPause())
            ijkVideoView.pause();
        playImg.setImageResource(R.drawable.icon_video_play);
    }
    //停止方法
    public void stop(){
        ijkVideoView.release(true);
    }
    //是否播放状态校验
    public boolean isPlaying(){
        if (ijkVideoView == null)
            return false;
        return ijkVideoView.isPlaying();
    }

    //隐藏封面
    public void setImageViewsetVisibility(int visibility){
        if (imageView == null)
            return;
        imageView.setVisibility(visibility);
    }
    //隐藏控制
    public void setMediaControllerVisibility(int visibility){
        if (relativeLayout == null)
            return;
        relativeLayout.setVisibility(visibility);
    }

    @OnLifecycleEvent(value = Lifecycle.Event.ON_PAUSE)
    private void onPause(){
        //暂停ijkplayer播放器
        pause();
    }

    @OnLifecycleEvent(value = Lifecycle.Event.ON_RESUME)
    private void onResume(){
        start();
    }

    @OnLifecycleEvent(value = Lifecycle.Event.ON_DESTROY)
    private void onDestroy(){
        //解除生命周期订阅
        AppCompatActivity appCompatActivity = (AppCompatActivity) getContext();
        appCompatActivity.getLifecycle().removeObserver(this);
        //释放掉ijk播放器
        ijkVideoView.release(true);
    }

    @Override
    public void onPrepared() {
        //准备完成->隐藏封面页
        setImageViewsetVisibility(View.GONE);
    }

    @Override
    public void onClick(View view) {
        if (ijkVideoView.isPlaying()){
            playImg.setImageResource(R.drawable.icon_video_play);
            ijkVideoView.pause();
        }else {
            playImg.setImageResource(R.drawable.icon_video_pause);
            ijkVideoView.start();
        }
    }
}
